//
//  CZQrcodeViewController.m
//  小和微博
//
//  Created by 董晓合 on 15/2/16.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZQrcodeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "CZMeViewController.h"

@interface CZQrcodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>
/**
 *  我的名片
 */
- (IBAction)me:(id)sender;

- (IBAction)closeBtnClick:(id)sender;
/**
 *  冲击波
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *qrcodeConsY;
/**
 *  会话
 */
@property (nonatomic, strong) AVCaptureSession *session;
/**
 *  预览界面
 */
@property (nonatomic, weak) AVCaptureVideoPreviewLayer *previewLayer;

@property (nonatomic, strong) CADisplayLink *link;

@end

@implementation CZQrcodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    创建定时器
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    self.link = link;
    
    
//    1.获取设备
    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
//    2.根据输入设备创建输入对象
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:inputDevice error:NULL];
    
//    3.创建输出对象
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    
//    4.设置输出对象的代理
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
//    5.创建会话
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    self.session = session;
    
//    6.将输入和输入添加到会话中
    if ([session canAddInput:input]) {
        [session addInput:input];
    }
    if ([session canAddOutput:output]) {
        [session addOutput:output];
    }
//    7.设置输出的数据类型
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    
//    8.设置预览界面
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
    previewLayer.frame = self.view.bounds;
    [self.view.layer insertSublayer:previewLayer atIndex:0];
    self.previewLayer = previewLayer;
    
//    9.开始采集数据
    [session startRunning];
    
    
}


- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {

//    1.判断是否解析到了数据
    if (metadataObjects.count > 0) {
//        停止扫描
        [self.session stopRunning];
//        移除预览界面
        [self.previewLayer removeFromSuperlayer];
//        取出数据
        AVMetadataMachineReadableCodeObject *obj = [metadataObjects lastObject];

//        停止动画
        [self.link invalidate];
        self.link = nil;
        
//        显示数据
        UILabel *lable = [[UILabel alloc] initWithFrame:self.view.bounds];
        lable.numberOfLines = 0;
        lable.text = obj.stringValue;
        [self.view addSubview:lable];
        
    }

}

- (void)update {

    self.qrcodeConsY.constant -= 5;
    if (self.qrcodeConsY.constant <= -170) {
        self.qrcodeConsY.constant = 170;
    }

}

- (IBAction)me:(id)sender {
    
    CZMeViewController *meVc = [[CZMeViewController alloc] init];
    [self.navigationController pushViewController:meVc animated:YES];
}

- (IBAction)closeBtnClick:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
